//
//  GetTradeDetailEngine.m
//  HuLaQuan
//
//  Created by liyan on 16/1/27.
//  Copyright © 2016年 yuwubao. All rights reserved.
//

#import "GetTradeDetailEngine.h"

#import "CurrentUser.h"

@implementation GetTradeDetailEngine


/**
 *  "1-交易状态":"Finished","7-银行名称":"农业银行","9-交易订单号":"1601110000015058","8-交易时间":"2016-01-11 11:00:36","6-交易方式":"622848*080140454475","2-状态说明":"交易成功","3-交易金额":"1","5-商家信息":null,"4-商品信息":"零食"
 *
 *  @param tradeId 交易订单号
 */
-(void)getTradeDetail:(NSString *)tradeId
{
    NSString *randomKey = [self createKeyWithLength:24];
    
    NSString *src = [NSString stringWithFormat: @"<?xml version=\"1.0\" encoding=\"UTF-8\"?><msg service=\"getTradeDetail\" sysName=\"\" sign=\"RSA\" serialNumber=\"%@\" termId=\"star_open_esa\" termAdd=\"23.23,23.45\" termBrand=\"iPhone\" osVersion=\"iPhone OS 9.1\" appVersion=\"1.0.0\" tokenValue=\"%@\" userId=\"%@\" channelId=\"88888888\"><tradeId>%@</tradeId></msg>",[self getSerialNumber],[CurrentUser sharedInstance].token,[CurrentUser sharedInstance].ID,tradeId];
    //
    NSLog(@"%@ -- src --- ",src);
    
    NSString *final = [NSString stringWithFormat:@"%@|%@|%@",[self firstString],[self secondStringWithKey:randomKey],[self thirdStringWithBody:src andKey:randomKey]];
    
    NSLog(@"%@",final);
    [YuHTTPClient XMLDataWithUrl:SERVER_HOST_NOTIFY parameters:final success:^(BOOL isSuccess, id item){
        
        NSDictionary *result;
        if (isSuccess) {
            
            NSString *resString = [_DES decrypt:[item lastObject] withKey:randomKey];
            
            NSDictionary *resultDic = [self parseXMLFromYinLianConnection:resString];
            
           // NSString *responseString;
            
            NSData *da= [resultDic[@"attach"] dataUsingEncoding:NSUTF8StringEncoding];
            
            NSError *error = nil;
            
            id jsonObject = [NSJSONSerialization JSONObjectWithData:da options:NSJSONReadingAllowFragments error:&error];
            
            NSLog(@"%@ %@",resString,jsonObject);
            
            result = @{@"isSuccess":@YES,@"result":jsonObject};
        }else{
            result = @{@"isSuccess":@NO,@"message":item};
          
        }

        //
        [[NSNotificationCenter defaultCenter] postNotificationName:@"GetTradeDetailFinish" object:result];
    }];
}
@end
